package org.smartercontext.contextmger.json.writer;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 
 * @author JuanPablo
 * @version 1.0
 * @creation_date 09 Mar 2014
 * This class allows write in JSON format an dataset about a user.
 */
public class JSONWriter implements IJSONWriter{

	@Override
	public String writeLocation(String latitude, String longitude, String user) {
		
		JSONObject objLocation = new JSONObject();
		try {
			objLocation.put("latitude",latitude);
			objLocation.put("longitude", longitude);
			objLocation.put("user", user);
		} catch (JSONException e) {
			
			System.out.println("Wrong creating JSON format");
		}
		
				
		return objLocation.toString();
	}

	@Override
	public String writeBuyerPreferences(List<String> wishes,
			List<String> productsSeen, List<String> purchased, String user) {
		
		JSONObject objectPreferences = new JSONObject();
		try {
			
			objectPreferences.put("username", user);
			
			JSONArray jsonArrayWish = new JSONArray();
			
			for(int i=0;i<wishes.size();i++){
				String prodWish = wishes.get(i);
				String split[]=prodWish.split("#");
				String name = split[0];
				String eanucc = split[1];
				String gtin8 = split[2];
				String gtin14 = split[3];
				
				JSONObject jsonObjTemp = new JSONObject();
				jsonObjTemp.put("product-name", name);
				jsonObjTemp.put("ean-ucc", eanucc);
				jsonObjTemp.put("gtin-8", gtin8);
				jsonObjTemp.put("gtin14", gtin14);
				
				jsonArrayWish.put(jsonObjTemp);
			}
			
			objectPreferences.put("wishlist", jsonArrayWish);
			
			JSONArray jsonArrayPSeen = new JSONArray();
			
			for(int i=0;i<productsSeen.size();i++){
				String prdSeen = productsSeen.get(i);
				String[] split = prdSeen.split("#");
				String name = split[0];
				String eanucc = split[1];
				String gtin8 = split[2];
				String gtin14 = split[3];
				
				JSONObject jsonObjTemp = new JSONObject();
				jsonObjTemp.put("product-name", name);
				jsonObjTemp.put("ean-ucc", eanucc);
				jsonObjTemp.put("gtin-8", gtin8);
				jsonObjTemp.put("gtin14", gtin14);
				
				jsonArrayPSeen.put(jsonObjTemp);
				
			}
			
			objectPreferences.put("products-seen", jsonArrayPSeen);
			
			JSONArray jsonArrayPRanked = new JSONArray();
			
			for(int i=0;i<purchased.size();i++){
				String prdRanked = purchased.get(i);
				String[] split = prdRanked.split("#");
				
				String value = split[0];
				String name = split[1];
				String eanucc = split[2];
				String gtin8 = split[3];
				String gtin14 = split[4];
				
				JSONObject jsonObjTemp = new JSONObject();
				jsonObjTemp.put("value", value);
				jsonObjTemp.put("product-name", name);
				jsonObjTemp.put("ean-ucc", eanucc);
				jsonObjTemp.put("gtin-8", gtin8);
				jsonObjTemp.put("gtin14", gtin14);
				
				jsonArrayPRanked.put(jsonObjTemp);
				
			}
			
			objectPreferences.put("products-ranked", jsonArrayPRanked);
			
			return objectPreferences.toString();
			
		} catch (JSONException e) {
			
			System.out.println("Wrong creating JSON format");
		}
		
		return null;
	}

	@Override
	public String writeLogin(String userName, String password) {
		
		JSONObject objLogin=new JSONObject();
		
		try{
			objLogin.put("userName", userName);
			objLogin.put("password", password);
		}catch (JSONException e) {
			
			System.out.println("Wrong creating JSON format");
		}
		
		return objLogin.toString();
	}

}
